/**
 * Created by lwy on 2017/7/23.
 */

//个人收益
var page = 1;
var configName;
var id;
var name,
    value,
    memo;

//编辑
$('#save2').click(function(){
    $.ajax({
        url:'/crm/userGiveC/edithUserGive.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            isValid:$('#editIfValid').val(),
            name:$('#editName').val(),
            money:$('#editMemo').val(),
            id:id,
        },
        type:'post',
        success:function(data){
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp";
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                alert('编辑成功');
                $('#myModal4').removeClass('fade in');
                $('#myModal4').addClass('fade');
                $('#myModal4').attr('aria-hidden',true);
                $('#myModal4').css('display','none');
                $('.modal-backdrop').css('display','none');
                page=1;
                doAjax();
            }
        }
    })
})

function doAjax(){
    $.ajax({
        url:'/crm/userGiveC/searchUserGive.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            isValid:$('#ifValid').val(),
            name:$("#configName").val(),
            type:'post',
            page:page,
        },
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp";
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.datas.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.datas.length;i++){
                    var ifValid = '';
                    if(data.datas.datas[i].isValid){
                        ifValid = '有效';
                    }else{
                        ifValid = '无效';
                    }
                    $('<tr class="gradeA odd">'+
                        '<td class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;">'+data.datas.datas[i].id+'</td>'+
                        '<td class=" ">'+data.datas.datas[i].name+'</td>'+
                        '<td class=" ">'+data.datas.datas[i].money+'</td>'+
                        '<td class=" ">'+data.datas.datas[i].createUserName+'</td>'+
                        '<td class=" ">'+ifValid+'</td>'+
                        '<td class="withdrawId"  data-toggle="modal" data-target="#myModal4" style="text-decoration: underline;cursor: pointer;" id="'+data.datas.datas[i].id+'" name="'+data.datas.datas[i].name+'" isValid="'+data.datas.datas[i].isValid+'" money="'+data.datas.datas[i].money+'">编辑</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }
                $(".pagination").createPage({
                    totalPage:data.datas.totalPage,
                    currPage:data.datas.pageNow,
                    turndown:'false',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
                //id处理
                $('.withdrawId').click(function(){
                    id = $(this).attr('id');
                    name = $(this).attr('name');
                    isValid = $(this).attr('isValid');
                    money=$(this).attr('money');
                    $('#editName').val(name);
                    $('#editValue').val(isValid);
                    $('#editMemo').val(money);
                    $('#editIfValid').empty();
                    console.log(isValid);
                    if(isValid=='true'){
                        console.log('1')
                        $('<option value="true">有效</option>' +
                            '<option value="false">无效</option>').appendTo($('#editIfValid'));
                    }else{
                        console.log('2')
                        $('<option value="false">无效</option>' +
                            '<option value="true">有效</option>').appendTo($('#editIfValid'));
                    }
                })
            }
        }
    })
}

$(function(){
    doAjax();
})

//查询
$('#refer').click(function(){
    page=1;
    doAjax();
})

//添加
$('#save').click(function(){
    $.ajax({
        url:'/crm/userGiveC/addUserGive.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            name:$('#addConfigName').val(),
            money:$('#addConfigValue').val(),
        },
        type:'post',
        success:function(data){
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp";
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                alert('添加成功');
                $('#myModal5').removeClass('fade in');
                $('#myModal5').addClass('fade');
                $('#myModal5').attr('aria-hidden',true);
                $('#myModal5').css('display','none');
                $('.modal-backdrop').css('display','none');
                page=1;
                doAjax();
            }
        }
    })
})